<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Map extends Controller
{
    public function index()
    {
       return $this->fetch('pages/map/map');
    }

    public function getYanjiaobun () {
		$result = Db::table('yanjiao')->field("concat_ws(', ', baidu_X, baidu_Y) as boundaries")->select();//燕郊边界
		//$result = Db::table('twolevel')->field("concat_ws(', ', baidu_X, baidu_Y) as boundaries")->select();//区边界
		//$result_for_street = Db::table('')->field("concat_ws(', ', baidu_X, baidu_Y) as boundaries")->select();//街道边界
		
    	$boundarr = array();
    	$len = count($result);
    	for($i = 0; $i < $len; $i++) {
    		$value = $result[$i]['boundaries'];
    		array_push($boundarr, $value);
    	}
    	$boundaries = join(';', $boundarr);
    	$req = array(
    		'boundaries'	=>	array(
    			$boundaries
    		)
    	);
    	$ut = new Utiltool($req);
    	return $ut->getdata();
	}
	public function getbulidingbun(){
        set_time_limit(120);      //执行时间
        ini_set('memory_limit', '4048M');    //内存
		$type_for_street = Db::table('twolevel')->field("street")->distinct(true)->select();//建筑边界
		$street_num = count($type_for_street);
		$data = array();
		for($j = 0; $j < $street_num ; $j++){
			$result_for_buildings = Db::table('twolevel')->field("line, point, name, street, concat_ws(', ', baidu_X, baidu_Y) as boundaries")
														->where('street',$type_for_street[$j]['street'])->select();//建筑边界
			$len = count($result_for_buildings);
			$store_for_buildings = array();
			$store_line = $result_for_buildings[0]['line'];//缓存线id
			$store_name = $result_for_buildings[0]['name'];//缓存线名字
			$store_for_building = array();
			//按格式处理数据
			for($i = 0; $i < $len ; $i++){
				if( $result_for_buildings[$i]['line'] != $store_line ){
					$str = join(';', $store_for_building);			
					$bulid = array(
						'name' => $store_name,
						'boundaries' => $str
					);
					array_push($store_for_buildings, $bulid );
					$store_for_building = [];
					$store_line = $result_for_buildings[$i]['line'];//缓存线id
					$store_name = $result_for_buildings[$i]['name'];//缓存线名字
				}
				array_push($store_for_building, $result_for_buildings[$i]['boundaries'] );
			}
			$str = join(';', $store_for_building);				
			$bulid = array(
				'name' => $store_name,
				'boundaries' => $str
			);
			array_push($store_for_buildings, $bulid );
			$store_for_building = [];
			$street = array(
				'name' => $type_for_street[$j]['street'],
				'childrenlist' => $store_for_buildings,
				'boundaries' => array()
			);
			array_push($data, $street);
		}
		$ut = new Utiltool($data);
    	return $ut->getdata();
	}
	public function getquarezhi(){
        set_time_limit(120);      //执行时间
        ini_set('memory_limit', '4048M');    //内存
		$type_for_street = Db::table('onelevel')->field("name")->distinct(true)->select();//建筑边界
		//var_dump($type_for_street);
		$len = count($type_for_street);
		$streets = array();
		for($i = 0; $i < $len; $i++){
			$result_for_street = Db::table('onelevel')->field("concat_ws(', ', baidu_X, baidu_Y) as boundaries")
														->where('name',$type_for_street[$i]['name'])->order('num desc')->select();
			$len_point = count($result_for_street);
			$point = [];
			for ($j = 0; $j < $len_point; $j++){
				array_push($point, $result_for_street[$j]['boundaries']);
			}
			$str = join(';',$point);
			$street = array(
				'name' => $type_for_street[$i]['name'],
				'boundaries' => $str
			);
			array_push($streets, $street);
		}
		$ut = new Utiltool($streets);
    	return $ut->getdata();
	}
	//目录
	public function getmenuitem(){
		$type_for_street = Db::table('onelevel')->field("name")->distinct(true)->select();
		$item = array();
		$len = count($type_for_street);
		for($i = 0; $i < $len; $i++){
			$item_index = array(
				'value' => $type_for_street[$i]['name'],
				'label' => $type_for_street[$i]['name'],
				'children' => array()
			);
			array_push($item, $item_index);
		}
		$menu = array(
			array(
				'value' => 'lefttop',
				'label' => '左上',
				'children' => array()
			),
			array(
				'value' => 'leftbot',
				'label' => '左下',
				'children' => $item
			)
		);
		$ut = new Utiltool($menu);
    	return $ut->getdata();
	}

}
